/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2025-03-02
 * Time: 21:23
 */
public class Main {
    //自己写的 不成功
    public void duplicateZeros(int[] arr) {
        int cur = 0;
        int dest = -1;
        int n = arr.length;
        while(cur < n){
            if(arr[cur] == 0){
                dest+=2;
            }else{
                dest++;
            }
            if(dest >= n-1){
                break;
            }
            cur++;
        }
        if(dest == arr.length){
            dest -= 2;
            cur--;
        }
        while(cur!=0 && dest != 0){
            if(arr[cur] != 0){
                arr[dest] = arr[cur];
                dest--;
            }else{
                arr[dest] = 0;
                arr[dest-1] = 0;
                dest-=2;
            }
            cur--;
        }
    }

    //老师写的
    public void duplicateZeros1(int[] arr) {
        int cur = 0,dest = -1,n = arr.length;
        //1.先找到最后一个需要复写的数
        while(cur < n){
            if (arr[cur] == 0){
                dest += 2;
            }else {
                dest++;
            }
            if (dest >= n - 1){
                break;
            }
            cur++;
        }
        //2.处理一下边界的情况
        if (dest == n){
            arr[n - 1] = 0;
            cur--;
            dest-=2;
        }
        //3.从后面向前完成复写操作
        while (cur >= 0){
            if (arr[cur] != 0){
                arr[dest--] = arr[cur--];
            }else {
                arr[dest--] = 0;
                arr[dest--] = 0;
                cur--;
            }
        }
    }
}
